﻿using Coldairarrow.Entity.DataView;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.DataView
{
    public partial class blade_visual_categoryBusiness : BaseBusiness<blade_visual_category>, Iblade_visual_categoryBusiness, ITransientDependency
    {
        public blade_visual_categoryBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<blade_visual_category>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<blade_visual_category>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<blade_visual_category, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<blade_visual_category> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task<List<blade_visual_category>> GetDataListAsync()
        {
            var q = GetIQueryable();

            return await q.ToListAsync();
        }

        public async Task AddDataAsync(blade_visual_category data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(blade_visual_category data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            string itemid = "";
            foreach (var item in ids)
            {
                itemid += "'" + item + "',";
            }
            itemid = itemid.Substring(0, itemid.Length - 1);
            string sqlMain = @"select blade_visual_category.Id from blade_visual_category INNER JOIN blade_visual ON blade_visual_category.category_value = blade_visual.category WHERE blade_visual_category.Id IN (" + itemid + ")";
            var list = await Db.GetDataTableWithSqlAsync(sqlMain);
            if (list.Rows.Count > 0)
            {
                throw new BusException("请先删除该类目下的大屏信息");
            }
            else
            {
                await DeleteAsync(ids);
            }


            
        }

        

        #endregion

        #region 私有成员

        #endregion
    }
}